package dom.xpath.query.xpath.internal;

import dom.xpath.data.Data;
import dom.xpath.index.Index;
import dom.xpath.query.xpath.XPContext;
import dom.xpath.query.xpath.values.NodeSet;
import dom.xpath.util.Token;

/**
 * This index class retrieves attribute values from the index.
 * 
 * @author Workgroup DBIS, University of Konstanz 2005-07, ISC License
 * @author Tim Petrowsky
 * @author Christian Gruen
 */
public final class IndexAccess extends InternalExpr {
  /** Index type. */
  private final Index.TYPE type;
  /** Token to be found in the index. */
  private final byte[] token;

  /**
   * Constructor.
   * @param typ index type
   * @param tok index token
   */
  public IndexAccess(final Index.TYPE typ, final byte[] tok) {
    token = tok;
    type = typ;
  }

  @Override
  public NodeSet eval(final XPContext ctx) {
    final Data data = ctx.local.data;
    ctx.local = new NodeSet(data.ids(type, token), ctx);
    return ctx.local;
  }

  @Override
  public String toString() {
    return getClass().getSimpleName() + "(" + type + ", \"" +
      Token.string(token) + "\")";
  }
}
